package bookdemo.controller;

import bookdemo.MPService.MPBookService;
import bookdemo.controller.utils.R;
import bookdemo.domain.Book;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
//mbp框架下用R封装的表现层

@CrossOrigin
@RestController
@RequestMapping("/books")
//@RequestMapping
public class BookController {
    @Autowired
    private MPBookService bookService;
//查询全部api
    @GetMapping
    public R getAll(){
        return new R(true,bookService.list());
    }
//修改内容api
    @PostMapping
    public R save(@RequestBody Book book){
//        R r = new R();
//        Boolean f = bookService.save(book);
//        r.setFlag(f);


        return new R(bookService.save(book));
    }
//添加内容api
    @PutMapping
    public  R update(@RequestBody Book book){

        return new R(bookService.update(book));
    }
//删除内容api
    @DeleteMapping("{id}")
    public  R delete(@PathVariable Integer id){

        return new R(bookService.delete(id));
    }
//id查询api
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){

        return new R(true,bookService.getById(id));
    }
//通过name分页查询
    @GetMapping("{name}")
    public R getname(@PathParam(value = "name") String name){

        return new R(true,bookService.getname(name));
    }
//    没有查询版本的分页查询
//    @GetMapping("{currentPage}/{pageSize}")
//    public R getPage(@PathVariable int currentPage,
//                               @PathVariable int pageSize){
//        //修复分页查询最后一页删除后，当前页码不更新bug
//        IPage<Book> page =bookService.getPage(currentPage,pageSize);
//        if(currentPage>page.getPages()){
//            page=bookService.getPage((int) page.getPages(),pageSize);
//        }
//        return new R(true,bookService.getPage(currentPage,pageSize));
//    }
//    有查询具体内容的分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,
                     @PathVariable int pageSize,
                     Book book){
        //修复分页查询最后一页删除后，当前页码不更新bug
        IPage<Book> page =bookService.getPage(currentPage,pageSize,book);
        if(currentPage>page.getPages()){
            page=bookService.getPage((int) page.getPages(),pageSize,book);
        }
        return new R(true,page);
    }

    /*分页查询*/
//    @GetMapping("/findpage")
//    public IPage<Book> findPage(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize,
//                                @RequestParam(defaultValue = "") String name//模糊搜索的参数
//    ){
//        IPage<Book> page = new Page<>(pageNum,pageSize);
//            QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
//        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
//        if (!"".equals(name)){
//            queryWrapper.like("name",name);//模糊搜索
//        }
//        lqw.like(Strings.isNotEmpty(book.getPress()),Book::getPress,book.getPress());
//        //条件查询，作者
//        lqw.like(Strings.isNotEmpty(book.getAuthor()),Book::getAuthor,book.getAuthor());
//        //条件查询，价格
//        lqw.like(Strings.isNotEmpty(book.getPrice()),Book::getPrice,book.getPrice());
//        //条件查询，出版年份
//        lqw.like(Strings.isNotEmpty(book.getYear()),Book::getYear,book.getYear())
//
//        return bookService.page(page,queryWrapper);

//    }

}
